//  
//  Pager.cs
//  
//  Author:
//       Tsukanov Nikita <keks9n@gmail.com>
// 
//  Copyright (c) 2010-2011 Tsukanov Nikita
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
// 
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.




using System;
using Gtk;
namespace SkypeTab
{
	[System.ComponentModel.ToolboxItem(true)]
	public partial class Pager : Gtk.Bin
	{

		public class PageInfo
		{
			public object Tag;
			public HBox HeaderBox;
			public Label HeaderLabel;
			public Label MenuLabel;
			public Fixed Area;
			public Image Notify;
			public Button CloseButton;
			private string m_Text;
			public int Height;
			public string Text
			{
				get
				{
					return m_Text;
				}
				set
				{
					var val=value;
					if(val==null)
						val="<unknown>";
					m_Text = val;
					
					var tmp = val;
					if (tmp.StartsWith ("*"))
						tmp = tmp.Substring (1);
					tmp = (tmp.Length > 15) ? tmp.Substring (0, 15) + "..." : tmp;
					HeaderLabel.Text = tmp;
					MenuLabel.Text = m_Text;
				}
			}
			public void SyncFont ()
			{
				var Font = "Default"; //Config.GetValue<string> ("Tabs", "Font", "Default");
				if (Font != "Default") {
					HeaderLabel.ModifyFont (Pango.FontDescription.FromString (Font));
				}
				int size = 16;//Config.GetValue<int> ("Tabs", "IconSize", 16);
				var ass = System.Reflection.Assembly.GetExecutingAssembly ();
				
				((Image)CloseButton.Image).Pixbuf = new Gdk.Pixbuf (ass, "SkypeTab.img.close_button.png").
					ScaleSimple (size, size, Gdk.InterpType.Bilinear);
				Notify.Pixbuf = new Gdk.Pixbuf (ass, "SkypeTab.img.icon_mod.png")
					.ScaleSimple (size, size, Gdk.InterpType.Bilinear);
				
				//Notify.Pixbuf = pixNotify.ScaleSimple (size, size, Gdk.InterpType.Bilinear);
				//(Image)CloseButton.Image).r
				
			}
			public void Init ()
			{
				Area.SizeAllocated += Sized;

			}
			private void Sized (object o, SizeAllocatedArgs args)
			{
				Height = args.Allocation.Y-2;
			}
		}
		public delegate void PageHandler(object sender, PageInfo page);
		public event PageHandler PageSelect;
		public event PageHandler PageClose;	
		public Pager ()
		{
			this.Build ();
			Notebook.CanFocus=false;
			Notebook.TabVborder=0;
			Notebook.Scrollable = true;
			Notebook.EnablePopup = true;
			
			Notebook.SwitchPage += HandleNotebookSelectPage;

		}

		
		
		void HandleNotebookSelectPage (object o, SwitchPageArgs args)
		{
			if (PageSelect != null)
			{
				PageSelect (this, ((Tagged)Notebook.CurrentPageWidget).PageInfo);
			}
		}
		private int FindPageByTag (object Tag)
		{
			for (int i = 0; i < Notebook.NPages; i++)
			{
				if (((Tagged)Notebook.GetNthPage (i)).PageInfo.Tag == Tag)
					return i;
			}
			return -1;
		}
		public bool ContainsTag(object Tag)
		{
			return FindPageByTag(Tag)!=-1;
		}
		
		public void ActivatePageByTag(object Tag)
		{
			Notebook.CurrentPage=FindPageByTag(Tag);
		}
		public void ClosePageByTag(object Tag)
		{
			ClosePage(FindPageByTag(Tag));
		}
		
		public PageInfo GetPageInfo (int Num)
		{
			return ((Tagged)Notebook.GetNthPage (Num)).PageInfo;
		}
		public PageInfo GetPageInfo (object Tag)
		{
			var ind = FindPageByTag (Tag);
			if (ind != -1)
				return GetPageInfo (ind);
			return null;
		}
		
		public int Count
		{
			get
			{
				return Notebook.NPages;
			}
		}
		public PageInfo ActivePage 
		{
			get
			{
				if (Notebook.NPages == 0)
					return null;
				return ((Tagged)Notebook.GetNthPage(Notebook.CurrentPage)).PageInfo;
			}
		}
		
		private class Tagged:Gtk.Fixed
		{
			public PageInfo PageInfo;
			
		}
		public PageInfo AddTab (string Header, object Tag)
		{
			return AddTab (Header, Tag, -1);
		}
		public PageInfo AddTab (string Header, object Tag, int Order)
		{
			Label headerLabel = new Label ();
			Label menuLabel = new Label ();
			HBox headerBox = new HBox ();
			Button closeBtn = new Button ();
			
			headerLabel.CanFocus=false;
			headerBox.CanFocus=false;
			menuLabel.CanFocus=false;
			closeBtn.CanFocus=false;
			
			closeBtn.Relief = ReliefStyle.None;
			
			
			var notifyImg = new Image ();
			closeBtn.Image = new Image ();
			
			headerBox.PackStart (notifyImg);
			headerBox.PackStart (headerLabel);
			headerBox.PackEnd (closeBtn);


			
			headerLabel.Show ();
			closeBtn.Show ();
			
			
			var widget = new Tagged ();
			widget.CanFocus=false;
			var PageInfo = new PageInfo ();
			PageInfo.Tag = Tag;
			PageInfo.HeaderBox = headerBox;
			PageInfo.HeaderLabel = headerLabel;
			PageInfo.MenuLabel = menuLabel;
			PageInfo.Area = widget;
			PageInfo.Text = Header;
			PageInfo.Notify = notifyImg;
			PageInfo.CloseButton = closeBtn;
			
			
			PageInfo.Init ();
			PageInfo.SyncFont ();
			
			
			widget.PageInfo = PageInfo;
			
			closeBtn.Clicked += delegate {
				if (PageClose != null)
					PageClose (this, PageInfo);
				Notebook.RemovePage (Notebook.PageNum (widget));
			
			};

			
			if (Order != -1)
				Notebook.InsertPageMenu (widget, headerBox,menuLabel, Order);
			else
				Notebook.AppendPageMenu (widget, headerBox, menuLabel);
			
			Notebook.ShowAll ();
			
			return PageInfo;
			
			
		
		}
		public void ClosePage (int i)
		{
			if(PageClose!=null)
				PageClose (this, ((Tagged)Notebook.GetNthPage (i)).PageInfo);
			Notebook.RemovePage (i);
		}
		public void RemovePageByTag (object Tag)
		{
			ClosePage (FindPageByTag (Tag));
		}
		
		public System.Collections.Generic.IEnumerable<PageInfo> GetPages()
		{
			for (int i = 0; i < Notebook.NPages; i++)
			{
				yield return ((Tagged)Notebook.GetNthPage (i)).PageInfo;
			}
			
		}
	}
	
}

